/**
 * 正方教务系统的菜单项工具，根据菜单的Elements返回成绩或者课表对应的Element元素，方便取得对应的URL
 */
package com.item.util;

import java.io.File;
import java.text.DecimalFormat;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;


public class MyFileUtil {
	
	public static String getFileSize(long size){
		DecimalFormat df = new DecimalFormat("#0.00");
		String result;
		double sizeDouble = (double)size;
		//不足1M
		if(size < ConstantResources.MBKIZE){
			result = df.format( sizeDouble/ ConstantResources.KBKIZE) + "KB";
		}else if(size < ConstantResources.GBKIZE){
			result = df.format(sizeDouble / ConstantResources.MBKIZE) + "M";
		}else{
			result = df.format(sizeDouble / ConstantResources.GBKIZE) + "G";
		}
		return result;
	}
	
	public static String getFileExt(String fileName){
		String fileExt="";
		if(fileName.contains(".")){
			fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
		}
		return fileExt;
	}
	
	private static String postAddFile(String path)throws Exception{
		String url = "http://121.40.157.14:8080/gs/fileUploadNew.do";
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		FileBody bin = new FileBody(new File(path));
		MultipartEntity reqEntity = new MultipartEntity();
		reqEntity.addPart("file", bin);
		post.setEntity(reqEntity);
		HttpResponse response;
		try{
			response = client.execute(post);
			HttpEntity entity = response.getEntity();
//			System.out.println(EntityUtils.toString(entity, "utf-8"));
			JSONObject obj = new JSONObject(EntityUtils.toString(entity, "utf-8"));
			return obj.getString("url");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			post.abort();
		}
		return "";
		
	}
	
	public static void main(String[] args){
		System.out.println(getFileSize(1473864));
	}
	
}
